<?php

namespace CtyunOss\api;

use AlexTartan\Array2Xml\XmlToArray;
use CtyunOss\config\OssConfig;
use CtyunOss\config\Request;
use CtyunOss\cors\OssHttp;
use CtyunOss\struct\ListAllMyBucketsResult;

/**
 * 天翼云OSS Service 工具类
 * @package    CtyunOss\service
 *
 * @author     Violet_Ice紫冰 <violetice@aliyun.com>
 * @create     2020-06-24 15:24:23
 */
class OssService
{
    /** @var OssConfig 天翼云OSS配置 */
    private $config;

    public function __construct(OssConfig $config)
    {
        $this->config = $config;
    }

    /**
     * 获取 Bucket 列表
     * @return ListAllMyBucketsResult
     * @throws \AlexTartan\Array2Xml\Exception\ConversionException
     * @throws \CtyunOss\exceptions\OssConfigException
     * @throws \CtyunOss\exceptions\OssHttpException
     * @throws \CtyunOss\exceptions\OssSignException
     * @throws \Symfony\Contracts\HttpClient\Exception\HttpExceptionInterface
     * @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface
     */
    public function getBuckets()
    {
        $request = new Request();
        $request->setHost($this->config->getEndpoint());
        $request->setUrl('/');

        $http = new OssHttp($request, $this->config);
        $res  = $http->send();
        $data = (new XmlToArray())->buildArrayFromString($res);
        return new ListAllMyBucketsResult($data['ListAllMyBucketsResult']);
    }
}